package com.gorkr.hot100.medium;

import org.junit.jupiter.api.Test;

/**
 * @author 张心宇 <zhangxinyu20@kuaishou.com>
 * Created on 2023-01-06
 */
public class M152maximunProductSubarray {

    public int maxProduct(int[] nums) {
        // dp 定义
        int[][] dp = new int[nums.length][2];
        // base case
        dp[0][0] = nums[0];
        dp[0][1] = nums[0];
        for (int i = 1; i < dp.length; i++) {
            if(nums[i]>0){
                dp[i][0] = Math.max(nums[i], nums[i] * dp[i-1][0]);
                dp[i][1] = Math.min(nums[i], nums[i] * dp[i-1][1]);
            }else {
                dp[i][0] = Math.max(nums[i], nums[i] * dp[i-1][1]);
                dp[i][1] = Math.min(nums[i], nums[i] * dp[i-1][0]);
            }
        }
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < dp.length; i++) {
            max = Math.max(max, dp[i][0]);
        }
        return max;
    }

    @Test
    void test(){
        System.out.println(maxProduct(new int[] {7, -2, -3}));
    }
}
